package calculation;

import java.text.DecimalFormat;
import java.util.Hashtable;
import java.util.Vector;

import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
/**
 * @author 
 * Calculation This class is used to calculate  
 */
public class Calculation 
{
	String Expression[] ;
	String Result;
	int PVOrder = 0;
	
	Vector<Object> ExpressionVector = new Vector<Object>();
	DBManager dbSQL;
	GetIdName gin;
	String Request;
	//String MasterPath = "C://Reports//Basic_Reports//Master_Reports//";
	//String fileFormat = ".txt";
//	ReadFile rFile = new ReadFile();
	
	public Hashtable<Object,Object> TransactionMap = new Hashtable<Object,Object>(); 
	public Hashtable<Object,Object>TransactionDetailMap =  new Hashtable<Object,Object>();
	String tableName;
	String trans_type;
	String transaction_type = "transaction_type";
	//FileUtility file;
	//RandomAccessFile raf;
	
	/**
	 * This metohod is calculate the contents of two Hashtable
	 * @param tableName Gets the specifed table name
	 * @param dbSQL Gets the object Reference of DBManager class
	 * @param TransactionDetailMap Gets the HashMap
	 * @param TransactionMap Gets the HashMap
	 */
	
	public Calculation(String tableName, DBManager dbSQL, Hashtable<Object,Object> TransactionMap, Hashtable<Object,Object>TransactionDetailMap,String Expressionbuffer[])
	{		
		this.dbSQL = dbSQL;
		gin = new GetIdName(dbSQL);
		this.tableName = tableName;
		this.TransactionMap.putAll(TransactionMap);
		this.TransactionDetailMap.putAll(TransactionDetailMap);
		getExpression(Expressionbuffer);		
	}
	public Calculation(pool.preprocess.operation.Expression expression)
	{
		this.dbSQL = expression.database;
		gin = new GetIdName(dbSQL);
		this.tableName = expression.tablename;
		this.TransactionMap =expression.pool.getmasterHashtabel();
		this.TransactionDetailMap=expression.pool.getDetailHashtabel();
		Request = expression.tablename;
		getExpression(expression.result);
		
	}
	void  getExpression(String expRecord[])
	{	
		System.out.println("calculation::Calculation::getExpression()");
		String expression_id = gin.getId("Expression");
		for(int i=0;i<expRecord.length;i++)
		{
			String [] Values = expRecord[i].split("\t");
			try
			{
				PVOrder = Column.pv_index; 
				expression_id = Values[PVOrder-1];
				
			}
			catch (Exception e) 
			{
				e.printStackTrace();
				expRecord[0]= "error";
			}
			Expression expr = new Expression(dbSQL,TransactionMap, TransactionDetailMap);
			if(expr.setExpression(expression_id))
				ExpressionVector.add(expr);
		}
	}
	
	/**	 
	 * getResult() This method generatr the result of calculation
	 * @return Hashtable<Object,Object> Prpoerty Map
	 */
	public Calculation getResult()
	{
		Hashtable<Object , Object> temp = new Hashtable<Object, Object>();
		Expression expr =null;
		for(int i=0;i<ExpressionVector.size();i++)
		{
			expr = (Expression)ExpressionVector.get(i);
		if(expr.Expressiontable.equalsIgnoreCase(tableName))
			{
				String lastValue="";	
				//expr.TransactionMap.putAll(temp);
				
				expr.TransactionDetailMap.putAll(TransactionDetailMap);
				expr.TransactionMap.putAll(TransactionMap);
				
				if(expr.TableId.equals("transaction"))
				{
					lastValue = String.valueOf(expr.parse(expr.Expression));
					double tempvalue =Double.parseDouble(lastValue);
					DecimalFormat de = new DecimalFormat("0.00");
					lastValue = de.format(tempvalue);
					if(expr.TableId.equalsIgnoreCase("transaction"))
						TransactionMap.put(expr.returnRecord.toLowerCase(), lastValue);
					else if(expr.TableId.equalsIgnoreCase("transaction_details"))
						TransactionDetailMap.put(expr.returnRecord.toLowerCase(), lastValue);
				}
				if(expr.TableId.equals("transaction_details"))
				{	
					lastValue = String.valueOf(expr.parse(expr.Expression));
					double tempvalue =Double.parseDouble(lastValue);
					DecimalFormat de = new DecimalFormat("0.00");
					lastValue = de.format(tempvalue);
					if(expr.TableId.equalsIgnoreCase("transaction"))
						TransactionMap.put(expr.returnRecord.toLowerCase(), lastValue);
					else if(expr.TableId.equalsIgnoreCase("transaction_details"))
						TransactionDetailMap.put(expr.returnRecord.toLowerCase(), lastValue);
				}
				
			}
		}
 		return this;
	}
	public static void main (String args[])
	{
		DBManager db = new DBManager();
		db.getConnect();
		Hashtable<Object,Object>TransactionDetailMap =  new Hashtable<Object,Object>();
		Hashtable<Object,Object> TransactionMap = new Hashtable<Object,Object>();
		
			TransactionDetailMap.put("pkg","2");	
			TransactionDetailMap.put("buyrate","24.76");
			TransactionDetailMap.put("amount","0");
			TransactionDetailMap.put("rate","26.00");
			TransactionDetailMap.put("item","4197");
			TransactionDetailMap.put("mrp","28.00") ;
			TransactionDetailMap.put("qty","10");
			
			TransactionMap.put("servicetax","0");
			TransactionMap.put( "id","316");
			TransactionMap.put("buyer","962");
			TransactionMap.put("transaction_type","68");
			TransactionMap.put("delivery_date","2007-11-27 11:11:19");
			TransactionMap.put("salestax","0");
			TransactionMap.put("amount","0");
			TransactionMap.put("seller","4379");
			TransactionMap.put("total_amount","0");
			TransactionMap.put("vat","0");
			TransactionMap.put("order_date","2007-11-27 11:11:19");
			TransactionMap.put("octroi","0");
			
			
		//Calculation cal = new Calculation("844",db);
	/*	Calculation cal = new Calculation("transaction_details",db,TransactionMap,TransactionDetailMap);	 
		TransactionDetailMap = cal.getResult();
		System.out.println(TransactionDetailMap);
		t.end();
		 TimerRecordFile timerFile=new TimerRecordFile("calculation","Calculation","main",t.duration());*/
	}
}
